<?php
namespace common\models;

use yii\base\Model;
use yii\web\UploadedFile;
/**
 * Created by PhpStorm.
 * User: hey_heyang
 * Date: 2019-09-18
 * Time: 14:22
 */
class UploadForm extends Model
{
    /**
     * @var UploadedFile
     */
    public $file;

    public function rules()
    {
        return [
            ['file', 'file', 'extensions' => ['png', 'jpg', 'gif'], 'maxSize' => 1024*1024*10,'checkExtensionByMimeType' => false]
        ];
    }


    /**
     * 用户上传图片
     * @param $dir
     * @param $files
     * @author hey_heyang(635870453@qq.com)
     * @return array|bool|\yii\db\ActiveRecord[]
     */
    public function upload($dir,$files)
    {
        if ($this->validate()) {
            $time=date('Ymd',time());
            $dir =$dir.$time.'/';
            if (!is_dir($dir)) {
                @mkdir($dir, 0777,true);
            }
            $sExtension = strrchr($files['name'],'.');
            $file_name = time().$sExtension;
            if(move_uploaded_file($files['tmp_name'], $dir.$file_name)){
                return $time.'/'.$file_name;
            }else{
                return false;
            }
        } else {
            return false;
        }
    }



}